<template>
  <div class="login-container">
    <el-form ref="form" :model="form" label-width="80px" class="login-form">
      <h2 class="login-title">登录系统</h2>
      <el-form-item label="用户名">
        <el-input v-model="form.userPhone"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.userPassword" type="password" auto-complete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="login('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import tim from "../tim";

export default {
  inject:['reload'],
  name: "Login",
  data() {
    return {
      form: {
        userPhone:'',
        userPassword:'',
      }
    }
  },
  methods:{
    async login(formName){
      const _this = this
      console.log(formName)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (_this.form.userPhone === 'admin' && _this.form.userPassword === '123456') {
            window.sessionStorage.setItem('token', 'admin')
            _this.$alert('登录成功!', '消息', {
              confirmButtonText: '确定',
              callback: action => {
                _this.$router.push('/admin/me')
              }
            })
          }
          else {
            _this.$axios.post('http://106.14.247.163:8080/account/webLogin', _this.form).then(async function (resp) {
              console.log(resp)
              if (resp.data.data) {
                let userId = resp.data.data.id
                let role = resp.data.data.role
                let userSig = ''
                await _this.$axios.get('http://106.14.247.163:8080/account/getUserSig/' + userId).then(resp2 => {
                  console.log(resp2)
                  userSig = resp2.data.data.userSig
                  window.sessionStorage.setItem('userid', userId)
                  window.sessionStorage.setItem('usersig', userSig)
                })
                window.sessionStorage.setItem('token', userSig)
                await _this.initUserInfo(userId, role)
                _this.$alert('登录成功!', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    if (role === 1) {
                      _this.$router.push('/cons/me')
                    } else {
                      _this.$router.push('/mentor/me')
                    }
                  }
                })
              } else {
                return _this.$message.error('登录失败！')
              }
            })

            // else{
            //   return _this.$message.error('登录失败！')
            // }
          }
        }
      })

    },
    async initUserInfo(userId,role){
      const _this = this
      if (role === 1){
        await _this.$axios.get('http://106.14.247.163:8080/consultant/getConsultantById/'+userId).then(resp=>{
          if (resp.data.data){
            window.sessionStorage.setItem('name',resp.data.data.userInfo.userName)
            window.sessionStorage.setItem('grade',resp.data.data.consultantInfo.consultantGrade)
          }
        })
      }
      else {
        await _this.$axios.get('http://106.14.247.163:8080/supervisor/getSupervisorById/'+userId).then(resp=>{
          if (resp.data.data){
            let name = resp.data.data.userInfo.userName
            let count = resp.data.data.supervisorInfo.countTotal
            let timeTotal = resp.data.data.supervisorInfo.timeTotal
            window.sessionStorage.setItem('name',name)
            window.sessionStorage.setItem('count',count)
            window.sessionStorage.setItem('timeTotal',timeTotal)
            window.sessionStorage.setItem('countToday',resp.data.data.countToday)
            window.sessionStorage.setItem('timeToday',resp.data.data.timeToday)
            window.sessionStorage.setItem('curSess',resp.data.data.currentSession)
          }
        })
      }
      let userName = window.sessionStorage.getItem('name')
      let userSig = window.sessionStorage.getItem('usersig')
      tim.login({userID:String(userId),userSig:userSig})


    }
  }
}
</script>

<style scoped>
  .login-form {
    width: 350px;
    margin: 160px auto; /* 上下间距160px，左右自动居中*/
    background-color: rgba(255, 255, 255, 0.8); /* 透明背景色 */
    padding: 30px;
    border-radius: 20px; /* 圆角 */
  }

  /* 背景 */
  .login-container {
    position: absolute;
    width: 100%;
    height: 100%;
    background: url("../assets/bg.jpg");
  }

  /* 标题 */
  .login-title {
    color: #303133;
    text-align: center;
  }
</style>